package com.example.demo2.controller;

import com.example.demo2.dao.TestMapper;
import com.example.demo2.service.ITestService;
import com.example.demo2.utils.HttpUtils;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Future;
import java.util.concurrent.ThreadPoolExecutor;

import static com.example.demo2.utils.HttpUtils.check;

@RestController
@ResponseBody
@RequestMapping("/test")
@Slf4j
public class TestController {

    @Autowired
    private ITestService testService;

    @Autowired
    private TestMapper testMapper;


    /**
     * http://127.0.0.1:8888/test/case1
     */
    @RequestMapping(value = "/case1", method = RequestMethod.GET)
    public Object case1() {
        log.info("------------------------------------------------");
        log.info("情况1");
        log.info("完全不使用@Transactional管理");
        testService.case1();
        return null;
    }

    /**
     * http://127.0.0.1:8888/test/case2
     */
    @RequestMapping(value = "/case2", method = RequestMethod.GET)
    public Object case2() {
        log.info("------------------------------------------------");
        log.info("情况2");
        log.info("使用@Transactional管理部分SQL，通过操作数据库之后的观察，来理解程序的行为");
        testService.case2();
        check();
        log.info("事物提交之后可以查到");
        testMapper.delete("case2");
        check();
        log.info("没有用@Transactional管理的可以直接查到");
        return null;
    }

    /**
     * http://127.0.0.1:8888/test/case3
     */
    @RequestMapping(value = "/case3", method = RequestMethod.GET)
    public Object case3() {
        log.info("------------------------------------------------");
        log.info("情况3");
        log.info("@Transactional 注解使用的位置");
        testService.case3();
        return null;
    }

    /**
     * http://127.0.0.1:8888/test/case4
     */
    @RequestMapping(value = "/case4", method = RequestMethod.GET)
    public Object case4() {
        log.info("------------------------------------------------");
        log.info("情况4");
        log.info("@Transactional 注解的传播方式");
        testService.case4();
        return null;
    }

    @RequestMapping(value = "/case0", method = RequestMethod.GET)
    public Object case0(@RequestParam(name = "flag") String flag) {
        log.info("------------------------------------------------");
        log.info("情况0");
        log.info("重现事故的发生");
        testService.case0(flag);
        return null;
    }

    @RequestMapping(value = "/case0a", method = RequestMethod.GET)
    public Object case0a() {
        log.info("------------------------------------------------");
        testService.case0a();
        return null;
    }

    /**
     * http://127.0.0.1:8888/test/case0b
     */
    @SneakyThrows
    @RequestMapping(value = "/case0b", method = RequestMethod.GET)
    public Object case0b() {
        testService.case0b();
        return null;
    }

}
